package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/openSession")
public class OpenSession extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        //通过请求创建一个会话
        HttpSession session = req.getSession();
        //输出会话
        PrintWriter writer = resp.getWriter();
        writer.println("向暂存区存入数据,会话的存活时间是10秒");
        writer.println("会话" + session);
        //设置会话的存活时间   10秒      单位秒
        session.setMaxInactiveInterval(10);
        //通过会话向暂存区里面放入值
        session.setAttribute("name","孙悟空");

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
